package com.net.wanjian.networkhospitalmanager.activity.skilltrain;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.ScanPortraitActivity;
import com.net.wanjian.networkhospitalmanager.activity.base.BaseActivity;
import com.net.wanjian.networkhospitalmanager.adapter.base.BaseViewPagerAdapter;
import com.net.wanjian.networkhospitalmanager.bean.StudentApplyReserveReturn;
import com.net.wanjian.networkhospitalmanager.consts.ErrorCodeReturn;
import com.net.wanjian.networkhospitalmanager.consts.SharedPreferencesKeyConst;
import com.net.wanjian.networkhospitalmanager.fragment.skilltrain.LaboratoryReservationFragment;
import com.net.wanjian.networkhospitalmanager.fragment.skilltrain.MyReservationFragment;
import com.net.wanjian.networkhospitalmanager.fragment.skilltrain.TeacherLabReserveFragment;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.netstatus.NetUtils;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.ToastUtil;

import butterknife.BindView;
import io.reactivex.observers.DisposableObserver;

public class SkillTrainActivity extends BaseActivity {


    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.top_title_tv)
    TextView topTitleTv;
    @BindView(R.id.skill_train_tablayout)
    TabLayout skillTrainTablayout;
    @BindView(R.id.skill_train_viewpager)
    ViewPager skillTrainViewpager;

    private Fragment[] mFragments;
    private String[] mTitles;

    LaboratoryReservationFragment laboratoryReservationFragment;
    MyReservationFragment myReservationFragment;
    TeacherLabReserveFragment teacherLabReserveFragment;
    private String labReserveID;

    @Override
    protected void initData() {
        topTitleTv.setText("实验室预约");
        topBackLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SkillTrainActivity.this.finish();
            }
        });

        if (SharedXmlUtil.getInstance().read(SharedPreferencesKeyConst.User_Identity_Tag_key, "").equals("1")) {
            mTitles = new String[]{"实验室预约", "与我相关"};
            laboratoryReservationFragment = LaboratoryReservationFragment.getInstance();
            teacherLabReserveFragment = TeacherLabReserveFragment.getInstance();
            mFragments = new Fragment[]{laboratoryReservationFragment, teacherLabReserveFragment};
        } else {
            mTitles = new String[]{"实验室预约", "我的预约"};
            laboratoryReservationFragment = LaboratoryReservationFragment.getInstance();
            myReservationFragment = MyReservationFragment.getInstance();
            mFragments = new Fragment[]{laboratoryReservationFragment, myReservationFragment};
        }


        BaseViewPagerAdapter pagerAdapter = new BaseViewPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        skillTrainViewpager.setOffscreenPageLimit(2);
        skillTrainViewpager.setAdapter(pagerAdapter);

//        skillTrainTablayout.setTabMode(TabLayout.MODE_FIXED);
//        skillTrainTablayout.setLayoutMode(TabLayout.MODE_SCROLLABLE);
        skillTrainTablayout.setupWithViewPager(skillTrainViewpager);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_skill_train;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    //刷新预约列表的方法，以废弃
   /* public void refreshStudentReserveList() {
        myReservationFragment.getStudentLabReserveHttpRequest();
    }

    public void refreshLabReserveList() {
        laboratoryReservationFragment.getLabreserveHttpRequest();
    }*/

    /**
     * adapter中调用扫描二维码
     *
     * @param labReserveID
     */
    public void scanQRcode(String labReserveID) {
        this.labReserveID = labReserveID;
//        new IntentIntegrator(this).initiateScan();

        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setPrompt("请扫描教师的二维码");
        integrator.setCaptureActivity(ScanPortraitActivity.class);
        integrator.initiateScan();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        final IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
//                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
            } else {
                //更新teacherfragment的adapter数据
                if (labReserveID != null) {
                    HttpUtil.replaceGuidTeacher(SharedXmlUtil.getInstance().getDeviceId(), SharedXmlUtil.getInstance().getToken(),
                            SharedXmlUtil.getInstance().getHospitalId(), SharedXmlUtil.getInstance().getUserIdentityId(), labReserveID, result.getContents().split("\\^")[2], new DisposableObserver<StudentApplyReserveReturn>() {
                                @Override
                                public void onNext(StudentApplyReserveReturn studentApplyReserveReturn) {
                                    if (studentApplyReserveReturn.getResult().equals(ErrorCodeReturn.replaceTeacherAlreadyexist + "")){
                                        ToastUtil.showToast("更换的老师已经存在该实验室预约中");
                                        return;
                                    }

                                    if (studentApplyReserveReturn.getResult().equals(ErrorCodeReturn.success + "")) {
                                        teacherLabReserveFragment.switchTeacherLabReserveHttpRequest();
                                    }
                                }

                                @Override
                                public void onError(Throwable e) {

                                }

                                @Override
                                public void onComplete() {

                                }
                            });
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    /**
     * 进入详情进行操作后更新列表
     */
    @Override
    protected void onResume() {
        super.onResume();
    }
}
